object UpdateHexunDay{
  def getstockCodeFromEastmoney={
    val p = """\((\d+)\)""".r
    val lines=io.Source.fromURL("http://quote.eastmoney.com/stocklist.html").getLines
    val buffer=collection.mutable.ArrayBuffer.empty[String]
    var start=false
    for(line <- lines){
      if(!start && line.contains("<div id=\"quotesearch\">")) start=true
      if(start && line.contains("<div class=\"qphox\">")) start=false
      if(start){
        var d = p findFirstMatchIn line
        if(d!=None) buffer+=d.get.group(1)
      }
    }
    buffer.map(a=>if(a>"5") "sh"+a else "sz"+a).sortBy(_.toString).toList
  }
  def saveGzipFile(name:String,lines:Seq[String]){
    var outfile = new java.io.FileOutputStream(name)
    var outxz = new java.util.zip.GZIPOutputStream(outfile)

    val writer = new java.io.PrintWriter(outxz)
    lines.foreach(writer.println)
    writer.close
    outxz.close
    outfile.close
  }
  def getDayfromHexun(ids:Seq[String],t:Int=15)={
    val ret1 = new scala.collection.mutable.ArrayBuffer[String]
    val id_str=ids.map(id=>(if(id>"sz000") "2_" else "1_")+id.substring(2)).mkString(",")
    for(times <- 1 to t;if(ret1.size==0)){
    try{
      val url=new java.net.URL("http://flashquote.stock.hexun.com/Stock_Combo.ASPX?mc="+id_str+"&dt=")
      val urlCon = url.openConnection()
      urlCon.setConnectTimeout(3000)
      urlCon.setReadTimeout( 1000 )
      val tt=io.Source.fromInputStream(urlCon.getInputStream)
      ret1 ++= tt.getLines
      tt.close
      //println(times)
    }catch{
        case a:Exception=>if(times==t)println(ids.mkString(","))
    }}
    val rr=if(ret1.size>0){
      ids.zip(ret1(0).split("\\);").map{line=>
        if(line.contains("--")) "" else {
          val dd=line.substring(line.indexOf("[")+2,line.indexOf("]")).replace("'","").split(",")
          Seq(dd(0),(dd(2).toDouble*1000).toLong,(dd(3).toDouble*1000).toLong,(dd(4).toDouble*1000).toLong,(dd(5).toDouble*1000).toLong,
          (dd(26).toDouble*100).toLong,(dd(27).toDouble*1000).toLong,(dd(1).toDouble*1000).toLong,
          (dd(32).toDouble*100).toLong,(dd(33).toDouble*100).toLong,dd(31).toDouble*10000 toLong).mkString(",")
        }
      })
    } else ids.map((_,""))
    rr.toMap
  }
  def getDayfromHexunConcurrent(stocks:Seq[String],size:Int=120)={
    val ret=scala.collection.mutable.Map.empty[String,String]
    import scala.actors.Actor._
    val groups=stocks.grouped(size).toList
    val caller = self
    groups.foreach{id=>
      actor {
        caller ! getDayfromHexun(id)
      }
    }

    for(i <- 1 to groups.size)
    receive {
      case lines:Map[String,String] => ret ++= lines
    }
    ret
  }
  def main(args: Array[String]) {
    val map=getDayfromHexunConcurrent(getstockCodeFromEastmoney.filter(a=>(a.startsWith("sh6")||a.startsWith("sz3")||
      a.startsWith("sz00")))++(1000001 to 1000999).map("sh"+_.toString.substring(1))++(399001 to 399999).map("sz"+_) distinct)
    val date=map("sh000001").substring(0,8)
    val ss=map.filter(a=>(! a._2.contains(",0,0,0,0,0,0,")) &&a._2.startsWith(date)).map(a=>a._1+","+a._2).toList.sorted
    saveGzipFile("day"+date+".txt.gz",ss)
  }
}